<template>
  <div id="userregisterPage">
    <h2 class="title">用户注册</h2>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      :model="formState"
      name="basic"
      label-align="left"
      :label-col="{ span: 50 }"
      :wrapper-col="{ span: 60 }"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: '请输入您的用户名！' }]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入账号"
        />
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码!' },
          { min: 8, message: '密码不能小于8位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入您的密码"
        />
      </a-form-item>
      <a-form-item
        label="确认密码"
        name="checkPassword"
        :rules="[
          { required: true, message: '请输入确认密码!' },
          { min: 8, message: '密码不能小于8位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.checkPassword"
          placeholder="请输入确认密码！"
        />
      </a-form-item>

      <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
        <a-checkbox v-model:checked="formState.remember">记住密码 </a-checkbox>
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 4, span: 20 }">
        <div class="button-container">
          <a-button type="primary" class="register-btn" html-type="submit"
            >注册</a-button
          >
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from "vue";
import { userRegister } from "@/api/getCurrentUserRequest";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";

interface FormState {
  username: string;
  password: string;
  checkPassword: string;
}

const formState = reactive<FormState>({
  username: "",
  password: "",
  checkPassword: "",
});
const router = useRouter();

/**
 * 用于表单提交执行登录
 * @param values
 */
const handleSubmit = async (values: any) => {
  //判读两次密码是否相同
  if (formState.password != formState.checkPassword) {
    message.error("两次输入密码不一致！");
    return;
  }
  const res = await userRegister(values);
  if (res.data.code === 0 && res.data.data) {
    message.success("登录成功");
    //注册成功跳转登录页面
    router.push({
      path: "/user/login",
      replace: true, //用户在登陆完之后不会回到登陆页面，而是其他新的页面
    });
  } else {
    message.error("注册失败" + res.data.description);
  }
};
const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};
</script>
<style scoped>
#userregisterPage {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #ffffff;
  padding-bottom: 20px;
}

.title-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 150px;
}

.title {
  font-size: 36px;
  text-align: center;
  margin-bottom: 24px; /* 调整标题与表单的间距 */
}

.register-btn {
  justify-content: center;
}

.button-container {
  display: flex;
}
</style>
